package com.password.desktop.service;

import com.password.entity.User;
import com.password.services.UserService;

/**
 * 单用户系统
 * 
 * @description
 * @author yzh
 * @date 2020年6月1日
 */
public class LoginService {

	// 当前登录用户
	private User currentUser;
	private final UserService userService = UserService.getUserServices();
	private final static LoginService loginService = new LoginService();

	public static LoginService builderLoginService() {
		return loginService;
	}

	private LoginService() {

	}

	/**
	 * 用户注册
	 * 
	 * @author yzh
	 * @date 2020年6月7日
	 */
	public User registerUser(String userName, String password) {
		User addUser = userService.addUser(userName, password);
		return addUser;
	}

	/**
	 * 游客登录
	 * 
	 * @description
	 * @author yzh
	 * @date 2020年6月1日
	 *
	 */
	public User loginOnGuest() {
		if (!userService.isExistUser("guest")) {
			userService.addUser("guest", "123456");
		}
		currentUser = userService.getUser("guest", "123456");
		return currentUser;
	}

	public User login(String user, String password) {
		currentUser = userService.getUser(user, password);
		return currentUser;
	}

	public void logout(String user) {
		if (currentUser.getUserName().equals(user)) {
			currentUser = null;
		}
	}

	public User getCurrentUser() {
		return currentUser;
	}

}
